import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:leader_help/utils/methodchannelmanager.dart';

import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'entrance/route/router_config.dart';
import 'utils/util.dart';

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  DateTime? lastTime;
  @override
  void initState() {
    super.initState();
    MethodChannelManager.initChannel();

  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: () => WillPopScope(
        child: RefreshConfiguration(
          hideFooterWhenNotFull: true, //列表数据不满一页,不触发加载更多
          child: GetMaterialApp(
            theme: tpThemeData,
            enableLog: kDebugMode,
            title: '领导应用万人助万企',
            builder: (ctx, child) {
              ScreenUtil.setContext(ctx);

              final easyLoading = EasyLoading.init();
              child = MediaQuery(
                //设置文字大小不随系统设置改变
                data: MediaQuery.of(ctx).copyWith(textScaleFactor: 1.0),
                child: GestureDetector(
                  onTap: () {
                    FocusScopeNode focus = FocusScope.of(context);
                    if (!focus.hasPrimaryFocus && focus.focusedChild != null) {
                      FocusManager.instance.primaryFocus?.unfocus();
                    }
                  },
                  child: child,
                  // ),
                ),
              );
              return easyLoading(context, child);
            },
            debugShowCheckedModeBanner: false,
            localizationsDelegates: const [
              RefreshLocalizations.delegate,
            ],
            initialRoute: RouterPath.splash,
            onGenerateRoute: RouterConfig.generateRoute,
            getPages: RouterPages.getPages,
          ),
        ),
        onWillPop: () async {
          if (lastTime != null) {
            if (DateTime.now().difference(lastTime!) >
                const Duration(seconds: 1)) {
              return true;
            }
          }
          lastTime = DateTime.now();

          EasyLoading.showToast("再次点击退出程序");
          return false;
        },
      ),
    );
  }
}
